﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;

namespace CIIS
{
    public partial class UserEdit : BaseForm
    {
        private t_sys_user user = null;
        public UserEdit()
        {
            InitializeComponent();
            LoadTree();
            lookUpEdit1.Properties.DisplayMember = "Name";
            lookUpEdit1.Properties.ValueMember = "Id";
        }
        public UserEdit(t_sys_user _user)
        {
            InitializeComponent();
            LoadTree();
            user = _user;
            textEdit1.Text = user.USER_NAME;
            textEdit2.Text = user.UserCode;
            textEdit3.Text = user.ID_CARD;
            textEdit4.Text = user.POLICE_NO;
            lookUpEdit1.EditValue = user.DEPT_ID;
            lookUpEdit1.Properties.DisplayMember = "Name";
            lookUpEdit1.Properties.ValueMember = "Id";
        }
        private void LoadTree()
        {
              var tmplist=  dao.GetListFromCache<Contract.Domain.Office>();
              IList<Contract.Domain.Office> list = new List<Contract.Domain.Office>();
              IList<Contract.Domain.Office> treeList = new List<Contract.Domain.Office>();
              for (var i = 0; i < tmplist.Count; i++)
              {
                  var o = tmplist[i] as Office;
                  list.Add(o);
              }
              var tmp = list.Where(m => string.IsNullOrEmpty(m.ParentId)).OrderBy(m => m.Seq).ToList();
              if (tmp.Count > 0)
              {
                  foreach (var o in tmp)
                  {
                      o.Name = "-"+o.Name;
                      treeList.Add(o);
                      loadChild(list,treeList, o, 1);
                  }
              }
              lookUpEdit1.Properties.DataSource = null;
              lookUpEdit1.Properties.DataSource = treeList;
          
        }
        private void loadChild(IList<Contract.Domain.Office> all,IList<Contract.Domain.Office>tree,Office current,int depth)
        {
            string s = "|-";
            for (var i = 0; i < depth; i++)
            {
                s += "-";
            }
            var tmp=all.Where(m=>!string.IsNullOrEmpty(m.ParentId)&&m.ParentId.Equals(current.Id)).OrderBy(m => m.Seq).ToList();
            if (tmp.Count > 0)
            {
                foreach (var o in tmp)
                {
                    o.Name = s+o.Name;
                    tree.Add(o);
                    loadChild(all, tree, o, depth + 1);
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (user == null)
            {
                user = new t_sys_user();
                user.State.MarkNew();
            }
            else
            {
                user.State.MarkDirty();
            }
            user.USER_NAME = textEdit1.Text;
            if (string.IsNullOrEmpty(user.USER_NAME))
            {
                MessageBox.Show("姓名不能空！");
                return;
            }
            user.ID_CARD = textEdit3.Text;
            if (string.IsNullOrEmpty(user.ID_CARD))
            {
                MessageBox.Show("身份证不能空！");
                return;
            }
            user.UserCode = textEdit2.Text;
            if (string.IsNullOrEmpty(user.UserCode))
            {
                MessageBox.Show("登陆账号不能空！");
                return;
            }
            var o = lookUpEdit1.GetSelectedDataRow() as Office;
            if (o != null)
            {
                user.DEPT_ID = o.Id;
            }
            else
            {
                MessageBox.Show("单位不能空！");
                return;
            }
            if (string.IsNullOrEmpty(user.PASSWORD))
            {
                user.PASSWORD = Utility.Util.GetEncryptedPassword("8888");
            }
            if (CurrentUser != null)
            {
                user.DJDW = CurrentUser.DEPT_ID;
            }
            user.POLICE_NO = textEdit4.Text;
            if (string.IsNullOrEmpty(user.POLICE_NO))
            {
                MessageBox.Show("警号不能空！");
                return;
            }
            try
            {
                dao.Dao.SaveOrUpdate(user);
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败，可能登录名重复。");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
